#include <QDebug>
#include "global.h"
#include "md5.h"

NSGlobal* gSys = NULL;

NSGlobal::NSGlobal()
{


}

NSGlobal::~NSGlobal()
{

}

QTextCodec* NSGlobal::m_pCodec = QTextCodec::codecForName("UTF-8");

QTextCodec* NSGlobal::textCodec()
{
    return m_pCodec;
}
void NSGlobal::setTextCodec(QTextCodec *c)
{
    if (c != NULL)
    {
        m_pCodec = c;
    }
}
QString NSGlobal::string(const char *s, int len)
{
    return m_pCodec->toUnicode(s,len);
}
QString NSGlobal::string(const QByteArray &s)
{
    return m_pCodec->toUnicode(s);
}
QByteArray NSGlobal::array(const QString &value)
{
    return m_pCodec->fromUnicode(value);
}
void NSGlobal::strcpy(char *dest, const QByteArray &src, int size)
{
    if (0 == size)
    {
        ::strcpy(dest, src.data());
    }
    else
    {
        ::strncpy(dest, src.data(), size);
    }
}
void NSGlobal::strcpy(char *dest, const QString &src, int size)
{
    if (0 == size)
    {
        ::strcpy(dest, array(src).data());
    }
    else
    {
        ::strncpy(dest, array(src).data(), size);
    }
}

QString NSGlobal::dateTimeToStr(const QDateTime &v)
{
    return QString().sprintf("%04d-%02d-%02d %02d:%02d:%02d", v.date().year(), v.date().month(), v.date().day(), v.time().hour(), v.time().minute(), v.time().second());
}

QDateTime NSGlobal::strToDateTime(const QString &v, const QDateTime &def)
{
    QDateTime dtm = QDateTime::fromString(v, "yyyy-MM-dd HH:mm:ss");
    return dtm.isValid() ? dtm : def;
}

QString NSGlobal::dateToStr(const QDate &v)
{
    return QString().sprintf("%04d-%02d-%02d", v.year(), v.month(), v.day());
}

QDate NSGlobal::strToDate(const QString &v, const QDate &def)
{
    QDate dtm = QDate::fromString(v, "yyyy-MM-dd");
    return dtm.isValid() ? dtm : def;
}

QString NSGlobal::Encrypt(QString origin)
{
    QByteArray baPass = origin.toAscii();
    MD5 pass(baPass.data(), baPass.length());
    QString sPass = QString(pass.toString().c_str());
    return sPass;
}
